package com.tqz.rc.spring.cloud.ribbon;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ServerList;
import com.tqz.rc.spring.cloud.discovery.TqzRcServiceDiscovery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.ribbon.PropertiesFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p>
 *
 * @author tianqingzhao
 * @since 2021/9/9 11:29
 */
@Configuration
public class TqzRcRibbonClientConfiguration {

    @Autowired
    private PropertiesFactory propertiesFactory;

    @Bean
    public ServerList<?> getServerList(IClientConfig clientConfig, TqzRcServiceDiscovery tqzRcServiceDiscovery) {
        if (this.propertiesFactory.isSet(ServerList.class, clientConfig.getClientName())) {
            ServerList serverList = this.propertiesFactory.get(ServerList.class, clientConfig,
                    clientConfig.getClientName());
            return serverList;
        }

        TqzRcServerList tqzRcServerList = new TqzRcServerList(tqzRcServiceDiscovery);
        tqzRcServerList.initWithNiwsConfig(clientConfig);
        return tqzRcServerList;
    }
}
